﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.Persistent;

public partial class System_RouteShift : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (txtHidOrg.Value == "")
        {
            txtHidOrg.Value = OrgHidValue;
            txtOrg.Text = OrgShowValue;
            OrgHelper.BindUsersDDL(MyStateManager.DomainID, OrgHidValue, ddlOldUser);
            OrgHelper.BindUsersDDL(MyStateManager.DomainID, OrgHidValue, ddlObjectiveUser);
        }

        if (!IsPostBack)
        {
            DataBind();
            InitForm();
        }
    }

    protected void InitForm()
    {
        OrgHelper.BindUsersDDL(MyStateManager.DomainID, OrgHidValue, ddlOldUser);
        OrgHelper.BindUsersDDL(MyStateManager.DomainID, OrgHidValue, ddlObjectiveUser);
    }

    protected void gvNewMobileUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RadioButton radObjective = (RadioButton)e.Row.FindControl("rdoObjective");

            radObjective.Attributes.Add("onclick", "setObjectiveRadio(this)");
        }
    }

    protected void gvOldMobileUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RadioButton radOld = (RadioButton)e.Row.FindControl("rdoOld");

            radOld.Attributes.Add("onclick", "setRadio(this)");
        }
    }

    protected void BindGrid()
    {
        System.Text.StringBuilder sbOld = new System.Text.StringBuilder();
        System.Text.StringBuilder sbObjective = new System.Text.StringBuilder();

        sbOld.AppendFormat(" MU.DOMAIN_ID={0} ", MyStateManager.DomainID);

        if (string.IsNullOrEmpty(OrgHidValue))
        {
            return;
        }

        sbOld.AppendFormat(" AND U.ORG_ID IN ({0})", OrgHidValue);
        sbObjective.Append(sbOld.ToString());

        if (!string.IsNullOrEmpty(ddlOldUser.SelectedValue))
        {
            sbOld.AppendFormat(" AND U.ID = '{0}'", ddlOldUser.SelectedValue);

        }

        if (!string.IsNullOrEmpty(ddlObjectiveUser.SelectedValue))
        {
            sbObjective.AppendFormat(" AND U.ID = '{0}'", ddlObjectiveUser.SelectedValue);
        }

        DataTable dtOld = new MobileUserManager().Load(sbOld.ToString());
        DataTable dtObjective = new MobileUserManager().Load(sbObjective.ToString());

        gvOldMobileUser.DataSource = dtOld;
        gvOldMobileUser.DataBind();
        gvNewMobileUser.DataSource = dtObjective;
        gvNewMobileUser.DataBind();
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void txtOrg_TextChanged(object sender, EventArgs e)
    {
        InitForm();
        BindGrid();
    }

    protected void gvNewMobileUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvNewMobileUser.PageIndex = e.NewPageIndex;
        BindGrid();
    }


    protected void gvOldMobileUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvOldMobileUser.PageIndex = e.NewPageIndex;
        BindGrid();
    }

    protected void btnShift_Click(object sender, EventArgs e)
    {
        decimal oldUserID = 0;
        decimal objectiveUserID = 0;
        decimal tempPersonID = 0;
        UserManager manager = new UserManager();

        int rowOldCount = gvOldMobileUser.Rows.Count;
        int rowObjectiveCount = gvNewMobileUser.Rows.Count;

        try
        {
            for (int i = 0; i < rowOldCount; i++)
            {
                RadioButton radOld = (RadioButton)gvOldMobileUser.Rows[i].FindControl("rdoOld");
                if (radOld != null && radOld.Checked)
                {
                    decimal.TryParse(gvOldMobileUser.DataKeys[i].Value.ToString(), out oldUserID);
                    break;
                }
            }


            for (int i = 0; i < rowObjectiveCount; i++)
            {
                RadioButton radObjective = (RadioButton)gvNewMobileUser.Rows[i].FindControl("rdoObjective");
                if (radObjective != null && radObjective.Checked)
                {
                    decimal.TryParse(gvNewMobileUser.DataKeys[i].Value.ToString(), out objectiveUserID);
                    break;
                }
            }

            if (oldUserID == 0 || objectiveUserID == 0)
            {
                ShowMessage("请选择进行转移的人员");
                return;
            }

            USERS userOld = manager.GetUserByID(oldUserID);
            USERS userObjective = manager.GetUserByID(objectiveUserID);

            if (userOld.ORGID != userObjective.ORGID)
            {
                ShowMessage("由于选择的人员不属于同一个组织，无法进行转移");
                return;
            }

            tempPersonID = userOld.PERSONID;
            userOld.PERSONID = userObjective.PERSONID;
            userObjective.PERSONID = tempPersonID;

            manager.Save(userOld);
            manager.Save(userObjective);

            BindGrid();

        }
        catch (Exception ex)
        {
            ShowMessage("操作失败。异常信息:" + ex.Message);
        }
    }

    protected void ddlOldUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        DDLIndexChanged();
    }

    protected void ddlObjectiveUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        DDLIndexChanged();
    }

    protected void DDLIndexChanged()
    {
        BindGrid();
        if (gvNewMobileUser.Rows.Count == 1 && gvOldMobileUser.Rows.Count == 1)
        {
            RadioButton rdoOld = (RadioButton)gvOldMobileUser.Rows[0].Cells[0].FindControl("rdoOld");
            RadioButton rdoObjective = (RadioButton)gvNewMobileUser.Rows[0].Cells[0].FindControl("rdoObjective");
            rdoOld.Checked = true;
            rdoObjective.Checked = true;
        }
    }
}
